package com.hmall.cart.listener;

import com.hmall.cart.service.ICartService;
import com.hmall.common.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import java.util.Set;

@Component
@RequiredArgsConstructor
public class OrderStatusListener {

    private final ICartService cartService;

    // 清空·购物车
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(value = "cart.clear.queue", durable = "true"),
            exchange = @Exchange(value = "trade.topic"),
            key = {"order.create"}
    ))
    //@Payload: 消息体  @Header: 消息头
//    public void OrderListener(@Payload Set<Long> itemIds, @Header("userId") Long userId){
//        UserContext.setUser(userId);
//        cartService.removeByItemIds(itemIds);
//    }
    public void OrderListener(Set<Long> itemIds){
        cartService.removeByItemIds(itemIds);
    }
}
